package com.zut.edu;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zut.edu.http.BaseCallback;
import com.zut.edu.http.OkhttpHelper;
import com.zut.edu.utils.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.Buffer;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class AppealActivity extends AppCompatActivity {
    @BindView(R.id.appeal_button)
    QMUIRoundButton appealbutton;
    @BindView(R.id.appeal_layout)
    QMUILinearLayout appeallayout;
    @BindView(R.id.appeal_text)
    EditText appealtext;
    String phone;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appeal);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        phone =intent.getStringExtra("phone");
        EditLayout();
        appealbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StringUtils.isEmpty(appealtext.getText().toString().trim())){
                    Toast.makeText(AppealActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }
                else postPassword(phone,appealtext.getText().toString().trim());
            }
        });

    }

    private void EditLayout(){
        appeallayout.setBorderColor(getResources().getColor(R.color.qmui_config_color_black));
        appeallayout.setBorderWidth(1);
        appeallayout.setRadius(25);
        appealbutton.setChangeAlphaWhenPress(true);

    }

    private void postPassword(String phone,String password){

        OkhttpHelper okhttpHelper = OkhttpHelper.getInstance();
        Map<String,Object> params = new HashMap<>(2);
        params.put("userAccount",phone);
        params.put("userPassword",password);
        okhttpHelper.post(StringUtils.BASE_URL+"/user/update", params, new BaseCallback<String>() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.e("onFailure", String.valueOf(e));
            }

            @Override
            public void onSuccess(@NotNull Call call, @NotNull Response response, String s, int code) throws IOException {
                Toast.makeText(AppealActivity.this,"密码已修改",Toast.LENGTH_LONG).show();
                Intent re = new Intent(AppealActivity.this,LoginActivity.class);
                re.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(re);
                Log.e("onSuccess", String.valueOf(s));
            }

            @Override
            public void onError(@NotNull Call call, @NotNull Response response, int code, Exception e) {
                Log.e("onError", String.valueOf(response));
            }
        });
    }
}
